package com.shopping.mall.core.common.file.service.impl;

import com.shopping.mall.auth.utils.UserUtils;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.FileUtils;
import com.shopping.mall.base.util.OSSUtils;
import com.shopping.mall.base.util.UUIDUtils;
import com.shopping.mall.core.common.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private OSSUtils ossUtils;

    @Override
    public Result<String> uploadPhoto(MultipartFile photo) throws IOException {
        if(!FileUtils.isImage(photo.getInputStream())){
            return Result.error(ResponseState.FILE_FORMAT_ERROR.getValue(),ResponseState.FILE_FORMAT_ERROR.getMessage());
        }
        String userId= UserUtils.getLoginUserId();
        String url=ossUtils.uploadFile(photo.getInputStream(),userId+"/"+ UUIDUtils.getRandUUID()+FileUtils.getFileSuffix(photo.getOriginalFilename()));
        return Result.OK(url);
    }
}
